package com.it2297.controller;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.it2297.main.App;
import com.it2297.model.Comment;
import com.it2297.model.Company;
import com.it2297.model.Job;
import com.it2297.model.Product;
import com.it2297.view.JobCreateJob;
import com.it2297.view.JobApplication;
import com.it2297.view.JobRemoveApplication;
import com.it2297.view.JobRemoveJob;
import com.it2297.view.JobSearchJob;
import com.it2297.view.CompanyViewAllJobsByCompany;
import com.it2297.view.JobViewJobApplicants;
import com.it2297.view.ManageApplication;
import com.it2297.view.ManageJobApplicants;
import com.it2297.view.ProductCommentView;
import com.it2297.view.ProductDetailDialog;
import com.it2297.view.ViewShortlistedApplications;

public class JobController extends JPanel {
	
	private Object[] selectedItems;
	
	
	private JobSearchJob view;
	private JobCreateJob view2;
	private JobRemoveJob view3;
	private CompanyViewAllJobsByCompany view4;
	private JobApplication view5;
	private JobViewJobApplicants view6;
	private JobRemoveApplication view7;
	private ManageApplication view8;
	private ManageJobApplicants view9;
	private ViewShortlistedApplications view10;
	
	
	
	public JobController(JobSearchJob sjt){
		this.view = sjt;
	}
	
	public JobController(JobCreateJob cjt){
		this.view2=cjt;
	}
	
	public JobController(JobRemoveJob rjt){
		this.view3=rjt;
	}
	
	public JobController(CompanyViewAllJobsByCompany vjt){
		this.view4=vjt;
	}
	
	public JobController(JobApplication ja){
		this.view5=ja;
	}
	
	public JobController(JobViewJobApplicants cva){
		this.view6=cva;
	}
	
	public JobController(JobRemoveApplication jra){
		this.view7=jra;
	}
	
	public JobController(ManageApplication ma){
		this.view8 = ma;
	}
	
	public JobController(ManageJobApplicants mja){
		this.view9=mja;
	}
	
	public JobController(ViewShortlistedApplications vsa){
		this.view10=vsa;
	}
	
	public ArrayList<String> searchForMatch(){
			Job job = new Job();
			job.setSearch(view.getSearch());
			ArrayList<String> match=job.retrieveSearch(job);
			view.setTitle(job.getTitle());
			return match;
			
		}
	
	public void retrieveFullJobDetails(){
		Job job = Job.retrieveDetails(view.getTitle());
		
		view.setDescription(job.getDescription());
		view.setRequirement(job.getRequirement());
		view.setDuty(job.getDuty());
		view.setIndustryType(job.getIndustryType());
		view.setEmploymentType(job.getEmploymentType());
		view.setStartTime(job.getWorkingStartTime());
		view.setEndTime(job.getWorkingEndTime());
		view.setSalary(Double.toString(job.getSalary()));
		view.setBenefit(job.getBenefits());
		view.setCompanyName(job.getCompanyName());
		view.setJobID(job.getJobId());

	}
	
	public void CreateJob(){
		try{
		Job job=new Job();
		job.setTitle(view2.getTitle());
		job.setDescription(view2.getDesc());
		job.setRequirement(view2.getReq());
		job.setDuty(view2.getDuty());
		job.setIndustryType(view2.getIT());
		job.setEmploymentType(view2.getET());
		job.setWorkingStartTime(view2.getWST());
		job.setWorkingEndTime(view2.getWET());
		job.setSalary(view2.getSalary());
		job.setBenefits(view2.getBenefit());
		job.setCompanyId(view2.getCID());
		
		
			if(Job.IsTitleExist(job.getTitle())!=true){	
				if(Job.IsTitleExist2(job.getTitle())!=true){
					Job.createJob(job);
					JOptionPane.showMessageDialog(null, "Job advertisement posted successfully!");
				}
				else{
					Job.createJob2(job);
					JOptionPane.showMessageDialog(null, "Job advertisement posted successfully!");
				}
			}
			else{
				JOptionPane.showMessageDialog(null, "Similar job title found, unable to register.");
			}
		}
		catch(NumberFormatException e){
			JOptionPane.showMessageDialog(null, "Check if salary is numeric or is empty.");
		}
		
	}
	
	public void getJobDetails(){
		Job job = Job.retrieveDetails(view3.getTitle());
		
		view3.setDescription(job.getDescription());
		view3.setRequirement(job.getRequirement());
		view3.setDuty(job.getDuty());
		view3.setIndustryType(job.getIndustryType());
		view3.setEmploymentType(job.getEmploymentType());
		view3.setStartTime(job.getWorkingStartTime());
		view3.setEndTime(job.getWorkingEndTime());
		view3.setSalary(Double.toString(job.getSalary()));
		view3.setBenefit(job.getBenefits());
		view3.setCompanyName(job.getCompanyName());
		view3.setJobId(job.getJobId());
	}
	
	public void updateStatusToInactive(){
		Job job=new Job();
		job.setTitle(view3.getTitle());	
		job.setJobId(view3.getJobId());
		Job.updateStatusToInactive(job);			
	}
	
	public ArrayList<String> getAllJobsByCompany(){
		Job job = new Job();
		//job.setCompanyName(view4.getCompanyName());
		return job.retrieveAllJobsByCompany();	
	}
	
	public void getAllJobDetails(){
		Job job = Job.retrieveDetails(view4.getTitle());
		
		view4.setDescription(job.getDescription());
		view4.setRequirement(job.getRequirement());
		view4.setDuty(job.getDuty());
		view4.setIndustryType(job.getIndustryType());
		view4.setEmploymentType(job.getEmploymentType());
		view4.setStartTime(job.getWorkingStartTime());
		view4.setEndTime(job.getWorkingEndTime());
		view4.setSalary(Double.toString(job.getSalary()));
		view4.setBenefit(job.getBenefits());
		view4.setCompanyName(job.getCompanyName());
		view4.setJobId(job.getJobId());

	}
	
	public void applyForJob(){
		Job job=new Job();

		job.setTitle(view5.getTitle());
		job.setApplicantNRIC(view5.getNRIC());
		job.setAdditionalInformation(view5.getInfo());

		Job.getJobID(job);
		
		
		if(Job.checkForDuplicateApplication(job)!=true){
			if(Job.checkForDuplicateApplication2(job)!=true){
				Job.applyForJob(job);
				JOptionPane.showMessageDialog(null, "Job application submitted successfully at: " + new Timestamp(System.currentTimeMillis()));
			}
			else{
				Job.applyForJob2(job);
				JOptionPane.showMessageDialog(null, "Job application submitted successfully at: " + new Timestamp(System.currentTimeMillis()));
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "You have already applied for this job using NRIC: " + view5.getNRIC() + ".");
		}
	}
	
	public ArrayList<String> getApplicants(){
		Job job = new Job();
		job.setJobId(view6.getJobId());
		ArrayList<String> applicants = job.retrieveApplicants(job);
		view6.setNoResult(job.getNoResult());
		return applicants;	
	}

	public void getApplicantDetails(){
		Job job = new Job();
		job.setApplicantNRIC(view6.getNRIC());
		job.setTitle(view6.getJobTitle());
		Job.retrieveApplicantDetails(job);
		
		view6.setName(job.getApplicantName());
		view6.setEmail(job.getEmail());
		//view6.setPhone(Integer.toString(job.getPhone()));
		view6.setPhone(job.getPhoneNum());
		view6.setCompanyName(job.getCompanyName());
		view6.setJobTitle(job.getTitle());
		view6.setDate(job.getDate());
		view6.setAdditionalInfo(job.getAdditionalInformation());
	}
	
	public void shortlistApplicant(){
		Job job = new Job();
		job.setApplicantNRIC(view6.getNRIC());
		job.setJobId(view6.getJobId());
		
		Job.shortlistApplicant(job);
	}
	
	public ArrayList<String> getAllApplicationsByNeedy(){
		Job job = new Job();
		job.setApplicantNRIC(view7.getNRIC());
		ArrayList<String> applicants = job.getAllJobApplicationsByNeedy(job);
		view7.setNoResult(job.getNoResult());
		return applicants;
		
	}
	
	public void getApplicationDetails(){
		Job job = new Job();
		job.setApplicantNRIC(view7.getNRIC());
		job.setTitle(view7.getJobTitle());
		Job.retrieveApplicantDetails(job);
		
		view7.setCName(job.getCompanyName());
		view7.setJobTitle(job.getTitle());
		view7.setJobID(job.getJobId());
	}
	
	public void updateApplicationStatusToInactive(){
		Job job=new Job();
		job.setJobId(view7.getJobId());
		job.setApplicantNRIC(view7.getNRIC());
		Job.updateApplicationStatusToInactive(job);
		
	
		
	}
	
	
	/*
	public ArrayList<String> getApplicationsOfNeedy(){
		Job job = new Job();
		//job.setApplicantNRIC(view8.getNRIC());
		ArrayList<String> applicants = job.getAllJobApplicationsByNeedy(job);
	//	view8.setNoResult(job.getNoResult());
		return applicants;
		
	}
	*/
	
	
	public int getTotalNumOfApplicationsByNeedy() {
		return Job.getTotalNumOfApplicationsByNeedy();
	}

	
	public ArrayList<Job> getMultiple(int position, int count) {
		return Job.getMultipleItem(position, count);
	//	return Product.getMultipleItem(position, count);
	}

	
	public boolean delete() {
		boolean isSuccessful = false;
		selectedItems = view8.getSelectedItems();

		for (Object selectedItem : selectedItems) {
			Job _selectedItem = (Job) selectedItem;
			
				if (_selectedItem.updateApplicationStatusToInactive((Job) selectedItem)) {
					isSuccessful = true;
				} else {
					isSuccessful = false;
					break;
				}
			
			
		}
		return isSuccessful;
	}

	public ArrayList<Job> getMultipleApplicants(int position, int count) {
		Job job = new Job();
		String title = view9.getJobTitle();
		return Job.getMultipleApplicants(position, count, title);
	//	return Product.getMultipleItem(position, count);
	}
	
	public int getTotalNumOfApplicantsOfJob() {
		Job job = new Job();
		String title = view9.getJobTitle();
		return Job.getTotalNumOfApplicantsOfJob(title);
	}
	
	
	public boolean shortlistApplicants(){
		boolean isSuccessful = false;
		selectedItems = view9.getSelectedItems();
		for(Object selectedItem : selectedItems){
			Job _selectedItem = (Job) selectedItem;
			
			if(_selectedItem.shortlistApplicant((Job) selectedItem)){
				isSuccessful = true;
			}
			else{
				isSuccessful = true;
				break;
			}	
		}
		return isSuccessful;	
	}	
	
	
	
	
	public int getTotalNumOfShortlistedByNeedy() {
		return Job.getTotalNumOfShortlistedByNeedy();
	}

	
	public ArrayList<Job> getMultipleShortlisted(int position, int count) {
		return Job.getMultipleShortlisted(position, count);
	//	return Product.getMultipleItem(position, count);
	}
}
